#include "mat.h"
#include <iostream>

int main(void)
{
    // 证明Mat的拷贝构造函数是浅拷贝
    ncnn::Mat cons1(10);
    std::cout << cons1.w << std::endl;
    std::cout << "cons1 address: " << cons1.data << std::endl;
    ncnn::Mat cons12(cons1);
    std::cout << "cons12 address: " << cons12.data << std::endl;
    ncnn::Mat cons13(cons1);
    std::cout << "cons13 address: " << cons13.data << std::endl;
    // 数据引用计数
    std::cout << "cons1 ref count: " << *cons1.refcount << std::endl;
    // Mat 的数据区域为data + ref count组成
    std::cout << cons1.w << " "<< cons1.cstep << " " << cons1.total() << std::endl;
    
    std::cout << "cons1释放" << std::endl;
    cons1.release();
    std::cout << cons1.w << " "<< cons1.data << std::endl;
    std::cout << cons12.w << " "<< cons12.data << std::endl;
    std::cout << cons13.w << " "<< cons13.data << std::endl;

    std::cout << "cons12释放" << std::endl;
    cons12.release();
    std::cout << cons1.w << " "<< cons1.data << std::endl;
    std::cout << cons12.w << " "<< cons12.data << std::endl;
    std::cout << cons13.w << " "<< cons13.data << std::endl;

    // 引用计数为1，真正释放内存
    std::cout << "cons13释放" << std::endl;
    cons13.release();
    std::cout << cons1.w << " "<< cons1.data << std::endl;
    std::cout << cons12.w << " "<< cons12.data << std::endl;
    std::cout << cons13.w << " "<< cons13.data << std::endl;

    return 0;
}
